﻿using UnityEngine;
using System.Collections;

public delegate void CommodityClickCallback(Commodity commodity);

public class CommodityView : MonoBehaviour {
	public CommodityClickCallback onCommodityClick;
	private UILabel _name;
	private UILabel _price;
	private UILabel _num;

	private Commodity _commodity;
	
	public Commodity commodity {
		set {
			_commodity = value;
			_name.text = _commodity.name;
			_price.text = _commodity.price.ToString();

			if (_num != null){
				_num.text = _commodity.num.ToString();
			}
		}
		get {
			return _commodity;
		}
	}

	void Awake () {
		_name = transform.FindChild("Name").GetComponent<UILabel>();
		_price = transform.FindChild("Price").GetComponent<UILabel>();

		Transform numTrans = transform.FindChild("Num");
		if (numTrans != null)
			_num = numTrans.GetComponent<UILabel>();

		UIEventListener.Get (gameObject).onClick += onClick;
	}

	private void onClick(GameObject btn)
	{
		if (onCommodityClick != null) {
			onCommodityClick(_commodity);	
		}
	}
	
}
